// 拼抢记录
$(function() {
		$("#searchid").linkbutton({
				text : '查询',
				iconCls : 'icon-search'
			});
	$('#pingqiangrecord').datagrid({
		title :'<div align="left">您所在的位置：限时抢购管理模块&nbsp;&nbsp;--&gt;&gt;&nbsp;&nbsp;抢购参加记录管理</div>',
		idField : 'id',
		nowrap : false,
		striped : true,
		collapsible : false,
		width : 1000,
		height : 400,
		url : '../tgBrandMember/search.shtml',
		frozenColumns : [[{
					field : 'ck',
					title : '选择',
					checkbox : true
				}]],
		columns : [[{
					title : '基本信息',
					colspan :7
				}, {
					field : 'opt',
					title : '操作',
					width : 160,
					align : 'center',
					rowspan : 2,
					formatter : function(value, rec) {
						if (rec.id == undefined)
							return null;
							else 
								return '<span onclick=Ondelete("'
									+ rec.id + '")><font style="cursor:pointer">&nbsp;删除</font></span></span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<select id="aud_'+rec.id +'"><option value="1">确认抢购</option><option value="2">撤销抢购</option></select>&nbsp;&nbsp;&nbsp; <span onclick=OnAutding("'
									+ rec.id+'","'+rec.userId + '")><font style="cursor:pointer">审核</font></span></span>';
					}
				}], [
				{
					field : 'type',
					title : '审核状态',
					align:'center',
					width : 100,
					formatter : function(value, rec) {
					     if(value==1){
						 return "<font color='green'>抢购已确认</font>";
						 }
						 if(value==2)
						 {
						  return "<font color='black'>抢购已撤销</font>";
						 }
						 if(value==0||value==''){
						 if(rec.userId=='0'||rec.userId=='')
						 	return  "<font color='#FB6704'>游客申请</font>";
						 	return  "<font color='red'>申请未确认</font>";
						 }
						 return '';
					}
				},{
					field : 'code',
					title : '商品名称',
					mapping : 'code',
					align:'center',
					width : 100
				}
				,{
					field : 'name',
					title : '真实姓名',
					mapping : 'name',
					align:'center',
					width : 100
					
				}
				,{
					field : 'tel',
					title : '手机',
					align:'center',
					width : 100
				},{
					field : 'telephoe',
					title : '备用电话',
					align:'center',
					width : 100
				},{
					field : 'userId',
					title : '用户身份',
					mapping : 'userId',
					align:'center',
					formatter : function(value, rec) {
						if(value==undefined)
						return null;
					     if(value==0||value==''||value==null) {
						     return  "游客";
					     }else{
						     return  "会员";
					     }
					     
					},
					width : 80
					
				},{
					field : 'creatTimeString',
					title : '抢购日期',
					align:'center',
					width : 160
				}

		]],
		pagination : true,
		rownumbers : true,
		queryParams : {
			createTime_startTime : '',
			createTime_endTime : '',
			partTime_startTime : '',
			partTime_endTime : ''
		},
		toolbar : [{
					id : 'btnsave',
					text : '删除拼抢记录',
					iconCls : 'icon-remove',
					handler : function() {
						var records = $('#pingqiangrecord').datagrid("getSelections");
						var ids = '';
						if (null == records || records == "") {
							alert('请选择要删除的记录');
							return;
						}
						if (confirm("确定删除此条信息吗?")) {
						for (var i = 0; i < records.length; i++) {
							if (records[i].id == undefined
									|| records[i].id == "undefined") {

								alert('此条记录已不存在');
								return;
							}
							ids += records[i].id;
							ids += '_';
						}

					   deleteById(ids);
					

					}
					}
				},'-',{
					id : 'btnclose',
					text : '  &nbsp;关  闭',
					iconCls : 'icon-cancel',
					handler : function() {
						window.self.close();
					}
				}]
	}

	);
    $(".searchcenter").keydown(function(){
	     if(event.keyCode==13)
         {
           onSearch();
         }
	    });
});

function Ondelete(id) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if (confirm("确定删除此条信息吗?")) {
        deleteById(id);
		
	}
}
function onSearch() {
	var queryParams = $('#pingqiangrecord').datagrid("options").queryParams;
	queryParams.createTime_startTime = $("#createTime_startTime").val();
	queryParams.createTime_endTime = $("#createTime_endTime").val();
	queryParams.partTime_startTime = $("#partTime_startTime").val();
	queryParams.partTime_endTime = $("#partTime_endTime").val();
	queryParams.categoryId=$("#categoryId").val();
	queryParams.bigCategory=$("#bigCategory").val();
	queryParams.autding=$("#autding").val();
	$('#pingqiangrecord').datagrid("reload");
}
function deleteById(id){
   $.ajax({
			url : "../tgBrandMember/deleteById.shtml?ids="+id,
			success : function(msg) {

				var obj = eval('(' + msg + ')');
				var result = obj["result"];
			    if(!result){
                         alert("删除成功失败");
                         return;
                     }
                  alert("删除成功");
				$('#pingqiangrecord').datagrid("reload");
            }
		});
}
function OnAutding(id,uid) {
	if (id == "undefined") {
		alert("此条记录已删除");
		return;
	}
	if(uid=='0'||uid==''){
		alert('游客申请，无需审核');
		return;
	}
	if (confirm("确定审核此条信息吗?")) {
        var m=document.getElementById('aud_'+id).value;
		var url1 = "../tgBrandMember/updateMessageById.shtml";
       
						$.ajax({
									url : url1,
									data : "id="+id+"&autding="+m ,
									cache:false,
									success : function(msg) {

										var obj = eval('(' + msg + ')');
				                       var result = obj["result"];
			                          if(!result){
                                            alert("审核失败");
                                           return;
						                     }
						                  alert("审核成功");
										$('#pingqiangrecord').datagrid("reload");
									}
								});
	}
}
 function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="tgAnnouncement.categoryId" id="categoryId" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}
function bigCategoryChange(){
	var bigCategoryValue = $("#bigCategory option:selected").val();
	if(bigCategoryValue != ""){
		$('#smallCategory').html('加载中... ...');
		$('#goods').html('');
		$('#goodsType').html('');
		cacheData = '';
		$.get('../category/category_loadCategoryByPid.shtml?id='+bigCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="categoryId" id="categoryId" onchange="smallCategoryChange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#smallCategory').html(str);
		})
	}
}

var cacheData;
function smallCategoryChange(){
	var smallCategoryValue = $("#categoryId option:selected").val();
	if(smallCategoryValue != ""){
		$('#goodsType').html('加载中... ...');
		$.get('../type/type_loadTypeByCategoryId.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="typeId" id="typeId" onchange="threechange();" ><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goodsType').html(str);
		})
		$('#goods').html('加载中... ...');
		$.get('../brand/findAllBrandByCategory.shtml?categoryId='+smallCategoryValue+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="brandId" id="brandId"><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			cacheData = str;
			$('#goods').html(str);
		})
	}
}
function threechange(){
	var value2 = $("#typeId option:selected").val();
	if(value2==undefined)value2='';
	if(value2 != ""){
		$('#goods').html('加载中... ...');
		$.get('../brand/findAllBrandByType.shtml?typeId='+value2+'&t='+new Date().getTime().toString(36),function(mes){
			var json = eval('('+mes+')');
			var str = '<select name="brandId" id="brandId"><option value="">请选择</option>';
			for(var i = 0; i < json.length; i++){
				str += '<option value="'+json[i]['id']+'">'+json[i]['name']+'</option>';
			}
			str+='</select>';
			$('#goods').html(str);
		})
	}else{
		$('#goods').html(cacheData);
	}
}